<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'look';
cfc('core')->checklogin();
$basic=$this->plugin_setting('basic');
if($_RQ['op']=='look'){
	$resultJson = [];
	$pid = intval($_RQ['pid']);
	$isgetred=pdo_getcolumn('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'id');
	$item=pdo_get('jy_quan_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','desc','mid','rednum','snsid','type')); 
	$red=pdo_get('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>0,'status'=>1),array('fee','setmid'));
  	$sender = pdo_get('core_members',array('id'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	$member = pdo_get('jy_quan_member',array('mid'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']),array('title', 'logo'));
  
  	$send_name = $member['title'];
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
  	$issendred=pdo_getsum('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'fee');
	$issendnum=pdo_count('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'id');
	$resultJson['sharedata']=array(
		'title'=> '红包来了',
		'desc'=> $item['title'],
		'link'=>custom_url('jy_quan/item//id='.$pid.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_quan/resource/images/h-480.png'),
	);
	//获取红包领取记录
    $red_given = pdo_getall('jy_quan_redlog', array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),array('mid', 'fee', 'createtime'),'','id desc');
	if (!empty($red_given)) {
              	$red_receivers = array_column($red_given, 'mid');
          		$red_receivers = pdo_getall('core_members', array('id' => $red_receivers), array('nickname', 'avatar', 'id'), 'id');
				foreach ($red_given as &$r) {
					$red_receivers[''.$r['mid']]['avatar']=tomedia($red_receivers[''.$r['mid']]['avatar']);
                	$r = array_merge($r, $red_receivers[''.$r['mid']]);
                }
    }
	$get_money=pdo_getcolumn('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'fee');
    $money = $get_money;
    $resultJson = array_merge($resultJson, [
			'isgetred'=>$isgetred,
            'red_isideal' => 'yes',
            'red_result' => 'success',
            'red_total_amount' => $red['fee'],
            'red_given_amount' => $issendred,
            'red_this_amount' => $money,
            'red_total_count' => $item['rednum'],
            'red_given_count' => $issendnum,
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title'],
            'red_given' => $red_given
    ]);
    exi($resultJson);
}
if($_RQ['op']=='getred'){ 
  	$resultJson = [];
	$pid = intval($_RQ['pid']);
	$isjielong=pdo_getcolumn('jy_quan_item_jieli',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	if(!$isjielong){
		exi('请先接龙，才能抢红包噢！','error');
	}
	$isgetred=pdo_getcolumn('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1),'id');
	if($isgetred){
		exi('您已抢过该红包！','error');
	}
  
	$item=pdo_get('jy_quan_item',array('id'=>$pid,'uniacid'=>$_SESSION['uniacid']),array('title','desc','mid','rednum','snsid','type')); 
	$red=pdo_get('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid'=>0,'status'=>1),array('fee','setmid'));
  	$sender = pdo_get('core_members',array('id'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	$member = pdo_get('jy_quan_member',array('mid'=>$red['setmid'],'uniacid'=>$_SESSION['uniacid']),array('title', 'logo'));
  
  	$send_name = $member['title'];
	$nickname  = $sender['nickname'];
  	$send_name = $send_name?$send_name:$nickname;
  	
  	$send_avatar = $member['logo'] ? $member['logo'] : $sender['avatar'];
  	$send_avatar = $send_avatar ? $send_avatar :  '../core/resource/images/nopic.jpg';
  	$resultJson['sharedata']=array(
		'title'=> '红包来了',
		'desc'=> $item['title'],
		'link'=>custom_url('jy_quan/item//id='.$pid.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_quan/resource/images/h-480.png'),
	);
	$ordersn=get_ordersn('jy_quan_redlog','ordersn');
	$issendred=pdo_getsum('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'fee');
	$issendnum=pdo_count('jy_quan_redlog',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),'id');
	// 剩余金额
  	$yufee=$red['fee']-$issendred;
  	// 剩余次数
	$yutotal=$item['rednum']-$issendnum;
	if($yutotal<1 && $yufee<=0){
		exi('噢！红包已抢完，下次再来吧！','error');
	}
	if($basic['isideal_money']){
		$params =array(
			'money'=>$yufee,
			'mid'=>$_SESSION['uid'],
			'total_num'=>$yutotal,
			'remark'=>'抢红包-'.$item['title'],
		);
     
		$get_money=cfc('red_pack')->get_money($params,1);
		//$get_money = 0.46666666;
      	if($get_money){
          $get_money = round($get_money, 2);
          
          $insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$pid,
				'setmid'=>$red['setmid'],
				'fee'=>$get_money,
				'status'=>1,
				'ordersn'=>$ordersn,
				'createtime'=>TIMESTAMP
		   );
		   pdo_insert('jy_quan_redlog',$insert);
          
        //获取红包领取记录
        $red_given = pdo_getall('jy_quan_redlog', array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),array('mid', 'fee', 'createtime'),'','id desc');
		if (!empty($red_given)) {
              	$red_receivers = array_column($red_given, 'mid');
          		$red_receivers = pdo_getall('core_members', array('id' => $red_receivers), array('nickname', 'avatar', 'id'), 'id');
              	foreach ($red_given as &$r) {
					$r['avatar']=tomedia($r['avatar']);
                	$r = array_merge($r, $red_receivers[''.$r['mid']]);
                }
        }
          $money = $get_money;
          $resultJson = array_merge($resultJson, [
            'red_isideal' => 'yes',
            'red_result' => 'success',
            'red_total_amount' => $red['fee'],
            'red_given_amount' => $issendred + $money,
            'red_this_amount' => $money,
            'red_total_count' => $item['rednum'],
            'red_given_count' => $issendnum + 1,
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title'],
            'red_given' => $red_given
            ]);
          exi($resultJson);
		} else{
			exi('红包获取失败','error');
		}
	}else{
		if($yutotal==1){
			$money=$yufee;
		}else{
			$rand=mt_rand(1,100);
			$max=2*($yufee/$yutotal);
			$money=($max*$rand)/100;
		}
      	// 取两位小数
      	$money = round($money, 2);
      
		$params =array(
			'openid'=>$_SESSION['openid'],
			'money'=>$money,
			'send_name'=>$send_name,
			'wishing'=>$item['desc'],
			'act_name'=>$item['title'],
			'mch_billno'=>$ordersn,
			'remark'=>'抢红包-'.$item['title'],
		);
		$res = cfc('wechat_pay')->redpack($params);
		if($res['result_code'] == 'SUCCESS'){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$pid,
				'setmid'=>$red['setmid'],
				'fee'=>$money,
				'status'=>1,
				'ordersn'=>$ordersn,
				'createtime'=>TIMESTAMP
		   );
		   pdo_insert('jy_quan_redlog',$insert); 
          //获取红包领取记录
          $red_given = pdo_getall('jy_quan_redlog', array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'mid <>'=>0,'status'=>1),array('mid', 'fee', 'createtime'),'','id desc');
			if (!empty($red_given)) {
              	$red_receivers = array_column($red_given, 'mid');
          		$red_receivers = pdo_getall('core_members', array('id' => $red_receivers), array('nickname', 'avatar', 'id'), 'id');
              	foreach ($red_given as &$r) {
					$red_receivers[''.$r['mid']]['avatar']=tomedia($red_receivers[''.$r['mid']]['avatar']);
                	$r = array_merge($r, $red_receivers[''.$r['mid']]);
                }
            }
          
          $resultJson = array_merge($resultJson, [
            'red_isideal' => 'yes',
            'red_result' => 'success',
            'red_total_amount' => $red['fee'],
            'red_given_amount' => $issendred + $money,
            'red_this_amount' => $money,
            'red_total_count' => $item['rednum'],
            'red_given_count' => $issendnum + 1,
            'red_sender_name' => $send_name,
            'red_sender_avatar' => $send_avatar,
            'red_title' => $item['title'],
            'red_given' => $red_given
            ]);
          
          exi($resultJson);
		}else{
			exi('红包获取失败:'.$res['result_code'],'error');
		}
	}
	
}